﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace HotelReservApp
{
    using HotelReservation;
    using HotelReservTest;

    class AppConsole
    {
        static void Main(string[] args)
        {
            string FilePath;
            if (args.Length == 0)
            {
                FilePath = "./../../config.xml";
            }
            else if (args.Length == 2 && args[0] == "/f")
            {
                FilePath = args[1];
            }
            else
            {
                Console.WriteLine("Usage: HotelReservApp /f [configFilePath] ");
                goto Quit;
            }



            HotelReservSrv HotelService = new HotelReservSrv();

            if (!HotelService.LoadConfig(FilePath))
                goto Quit;

            Console.WriteLine("\nThe Hotel Reservation System Started.");

            while (true)
            {
                Console.WriteLine("\n\nPlease Input Query: \n");

                string InputQuery = Console.ReadLine();
                string LowerCaseQuery = InputQuery.ToLower();

                if (LowerCaseQuery == ".exit")
                    break;

                if (LowerCaseQuery == ".reload")
                {
                    Console.WriteLine("Reload Config File To Update The App On-the-fly");

                    if (!HotelService.LoadConfig(FilePath))
                    {
                        Console.WriteLine("The config file has errors, reload cancelled.");
                    }
                    continue;
                }

                if (LowerCaseQuery == ".runtest")
                {
                    UnitTest.Start();
                    continue;
                }

                string[] PriceTypeAndDays = LowerCaseQuery.Split(':');
                if (PriceTypeAndDays.Length != 2)
                {
                    foreach (string debug in PriceTypeAndDays)
                        Console.WriteLine(debug);

                    Console.WriteLine("Woo, We are unable to understand the query.");
                    continue;
                }

                string PricePolicy = PriceTypeAndDays[0].Trim();

                List<DateTime> days = new List<DateTime>();
                bool FoundError = false;

                string QueryDays = PriceTypeAndDays[1];
                string[] DaysArray = QueryDays.Split(',');
                foreach (string d in DaysArray)
                {
                    string OneDateTime = d.Trim();
                    if (OneDateTime.Length == 0)
                        continue;

                    Console.WriteLine("The Input Day is " + OneDateTime);

                    Match m = Regex.Match(OneDateTime, @"(\d+)([a-z]+)(\d+)\(([a-z]+)\)");

                    if (m.Success)
                    {
                        int Day = Convert.ToInt16(m.Groups[1].Value);
                        int Month = Utils.Month(m.Groups[2].Value);
                        int Year = Convert.ToInt16(m.Groups[3].Value);
                        int DayOfWeek = Utils.Weekday(m.Groups[4].Value);

                        DateTime dt = new DateTime(Year, Month, Day);
                        if ((Int16)dt.DayOfWeek == DayOfWeek)
                        {
                            days.Add(dt);
                        }
                        else
                        {
                            Console.WriteLine("The Date (" + OneDateTime + ") does not match its day of week: " + Utils.Weekday(m.Groups[4].Value));
                            FoundError = true;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Incorrect DateTime Format, Use format like '16Mar2009(mon)'");
                        FoundError = true;
                    }
                }

                if (!FoundError)
                {
                    string FoundHotel = HotelService.FindTheCheapestHotel(days, PricePolicy);
                    Console.WriteLine("\nCongrats! Found the cheapest Hotel: \n\n    " + FoundHotel);
                }
            }

        Quit:
            Console.Write("\nExit, Press Any Key To Return>");
            Console.ReadKey();
        }
    }
}